package br.ufrgs.tcp;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import br.ufrgs.tcp.db.AppSQLHelper;
import br.ufrgs.tcp.maps.AbstractItemizedOverlay;
import br.ufrgs.tcp.maps.PersonItemizedOverlay;
import br.ufrgs.tcp.maps.PrediosItemizedOverlay;
import br.ufrgs.tcp.model.Predio;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapFragment extends Fragment  {

	private MapView mapView;
	private Drawable predioIcon;
	private List<Overlay> overlays;
	private MapController mapController;
	private Drawable personIcon;
	private LocationManager locationManager;
	private LocationListener listener;
	private PersonItemizedOverlay pessoa;
	
	@Override
	public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
		mapView  =  (MapView) arg0.inflate(R.layout.map, arg1,false);
		mapView.setBuiltInZoomControls(true);
		overlays = mapView.getOverlays();
		mapController = mapView.getController();
		mapController.setZoom(21);
		return mapView;
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
		
		listener = new LocationListener() {
		

			@Override
			public void onLocationChanged(Location location) {
				if (pessoa != null) 
					overlays.remove(pessoa);
				pessoa = new PersonItemizedOverlay(personIcon, getActivity());
				pessoa.addOverlayItem(new OverlayItem(new GeoPoint((int) (location.getLatitude()*1e6), (int) (location.getLatitude()*1e6)), null, null));
				overlays.add(pessoa);			
				mapView.invalidate();
				
			}

			@Override
			public void onProviderDisabled(String arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onProviderEnabled(String arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				// TODO Auto-generated method stub
				
			}
			
		};
		
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);
		
		predioIcon = this.getResources().getDrawable(android.R.drawable.btn_star_big_on);
		personIcon = this.getResources().getDrawable(android.R.drawable.btn_radio);
	}

	@Override
	public void onResume() {
		super.onResume();
		populatePredios();
	}
	
	@Override
	public void onPause() {
		locationManager.removeUpdates(listener);
		super.onPause();
	}
	
	public void populatePredios() {

		Cursor prediosCursor = Predio.fetchAll();
		AbstractItemizedOverlay predios = new PrediosItemizedOverlay(predioIcon,getActivity());

		int latitudeColumnIndex = prediosCursor.getColumnIndex(AppSQLHelper.PREDIO_KEY_LATITUDE);
		int longitudeColumnIndex = prediosCursor.getColumnIndex(AppSQLHelper.PREDIO_KEY_LONGITUDE);
		int numeroColumnIndex = prediosCursor.getColumnIndex(AppSQLHelper.KEY_NUMERO);
		int enderecoColumnIndex = prediosCursor.getColumnIndex(AppSQLHelper.PREDIO_KEY_ENDERECO);

		if (prediosCursor.moveToFirst()) {
			do {
				predios.addOverlayItem(new OverlayItem(new GeoPoint(prediosCursor.getInt(latitudeColumnIndex), prediosCursor.getInt(longitudeColumnIndex)),
						String.valueOf(prediosCursor.getInt(numeroColumnIndex)),
						prediosCursor.getString(enderecoColumnIndex)));
				mapController.setCenter(new GeoPoint(prediosCursor.getInt(latitudeColumnIndex), prediosCursor.getInt(longitudeColumnIndex)));
				prediosCursor.moveToNext();
			} while (!prediosCursor.isAfterLast());
			
		}
		overlays.clear();
		if (predios.size() > 0) overlays.add(predios);
		mapView.invalidate();
		prediosCursor.close();
		Predio.close();
	}

}